﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ES.Admin
{
    using System.Linq.Expressions;

    public partial class FrmKategoriler : Form
    {
        public FrmKategoriler()
        {
            InitializeComponent();
        }

        private void FrmKategoriler_Load(object sender, EventArgs e)
        {
            Tazele();
        }

        private void Tazele(Expression<Func<Category, bool>> expression = null)
        {
            var categoriList = Program.Db.Categories.AsQueryable();
            if (expression != null)
                categoriList = categoriList.Where(expression);
            dataGridView1.DataSource = categoriList.Select(d => new { Kimlik = d.Id, KategoriAdi = d.Text, Seo = d.SeoText, Sırası = d.OrderNumber }).ToList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var frm = new YeniKategori();
            frm.ShowDialog();
            Tazele();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var id = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            var frm = new YeniKategori { Id = id };
            frm.ShowDialog();
            Tazele();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Tazele(d => d.Text.Contains(textBox1.Text) || d.SeoText.Contains(textBox1.Text));
        }
    }
}
